//
//  ZYphoneRecoverView.m
//  YiLin
//
//  Created by Zyark on 2018/5/29.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYPhoneRecoverView.h"
#import "JKCountDownButton.h"
#import "ZYImageUtils.h"

@interface ZYPhoneRecoverView()
@property(nonatomic,strong) UITextField *usernameTextField;
@property(nonatomic,strong) UITextField *passwordTextField;
@property(nonatomic,strong) UITextField *rePasswordTextField;
@property(nonatomic,strong) UITextField *codeTextField;
@property(nonatomic,strong) UIButton *confirmBtn;
@property(nonatomic,strong) UIButton *codeBtn;
@end

@implementation ZYPhoneRecoverView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

-(void)setUp{
    UITextField *usernameTextField = [[UITextField alloc] init];
    UITextField *passwordTextField = [[UITextField alloc] init];
    UITextField *codeTextField = [[UITextField alloc] init];
    UITextField *rePasswordTextField = [[UITextField alloc] init];
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    JKCountDownButton *codeBtn = [JKCountDownButton buttonWithType:UIButtonTypeCustom];
    codeBtn.frame =CGRectMake(0, 0, ZYSCREEN_W * 0.25, ZYSCREEN_W * 0.08);
    codeBtn.layer.cornerRadius = 3;
    codeBtn.layer.masksToBounds = YES;
    codeBtn.backgroundColor = kMainColor;
    [codeBtn setBackgroundImage:[ZYImageUtils imageWithColor:kMainColor rect:codeBtn.bounds] forState:UIControlStateNormal];
    codeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [codeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [codeBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    CGFloat left = ZYSCREEN_W * 0.08;
    CGFloat height = ZYSCREEN_W * 0.1;
    CGFloat padding = ZYSCREEN_W * 0.1;
    
    //设置渐变图片
    [ZYUtils setGradualChangingColorWithButton:confirmBtn size:CGSizeMake(self.width - left * 2, height)];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    confirmBtn.layer.cornerRadius = 5;
    confirmBtn.layer.masksToBounds = YES;
    
    //按钮点击
    [confirmBtn addTarget:self action:@selector(doConfirm:) forControlEvents:UIControlEventTouchUpInside];
    [codeBtn addTarget:self action:@selector(doGetCode:) forControlEvents:UIControlEventTouchUpInside];
    
    //初始化textField
    [passwordTextField setSize:CGSizeMake(self.width - left * 2, height)];
    [usernameTextField setSize:CGSizeMake(self.width - left * 2, height)];
    
    passwordTextField.secureTextEntry = YES;
    rePasswordTextField.secureTextEntry = YES;
    usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    codeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    rePasswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    
    [self addSubview:usernameTextField];
    [self addSubview:passwordTextField];
    [self addSubview:rePasswordTextField];
    [self addSubview:codeTextField];
    [self addSubview:confirmBtn];
    [self addSubview:codeBtn];
    
    ZYWeakSelf(weakSelf)
    
    [usernameTextField  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.left.mas_equalTo(left);
        make.top.mas_equalTo(30);
        make.height.mas_equalTo(height);
    }];
    
    [codeTextField  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.left.mas_equalTo(left);
        make.top.mas_equalTo(usernameTextField.mas_bottom).offset(padding * 0.5);
        make.height.mas_equalTo(height);
    }];
    
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.left.mas_equalTo(left);
        make.top.mas_equalTo(codeTextField.mas_bottom).offset(padding * 0.5);
        make.height.mas_equalTo(height);
    }];
    
    [rePasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.left.mas_equalTo(left);
        make.top.mas_equalTo(passwordTextField.mas_bottom).offset(padding * 0.5);
        make.height.mas_equalTo(height);
    }];
    
    
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.left.mas_equalTo(left);
        make.top.mas_equalTo(rePasswordTextField.mas_bottom).offset(padding);
        make.height.mas_equalTo(height);
    }];
    
    
    _confirmBtn = confirmBtn;
    _usernameTextField = usernameTextField;
    _passwordTextField = passwordTextField;
    _codeTextField = codeTextField;
    _rePasswordTextField = rePasswordTextField;
    _codeBtn = codeBtn;
    
    [self setupTextField:_usernameTextField imageName:@"ic_login_username_left"];
    [self setupTextField:_passwordTextField imageName:@"ic_login_password_left"];
    [self setupTextField:_codeTextField imageName:@"ic_login_code_left"];
    [self setupTextField:_rePasswordTextField imageName:@"ic_login_password_left"];
    
    [self setRightView:usernameTextField rightView:codeBtn];
}

-(void)setTextFieldLine:(UITextField *)textField{
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = RGBA(235, 235, 235, 1);
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textField);
        make.right.equalTo(textField);
        make.top.equalTo(textField.mas_bottom);
        make.height.equalTo(@1);
    }];
}


/**
 统一初始化
 @param textField textFiled
 @param imageName 左视图图片名称
 */
-(void)setupTextField:(UITextField *)textField imageName:(NSString *)imageName{
    [self setTextFieldLine:textField];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self setLeftImageView:textField imageName:imageName];
}


/**
 设置左视图
 @param textField texField
 @param imageName 显示的图片
 */
-(void)setLeftImageView:(UITextField *)textField imageName:(NSString *)imageName{
    UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
//    leftImageView.frame = CGRectMake(0, 0, 17, 21);
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ZYSCREEN_W * 0.08, ZYSCREEN_W * 0.26)];
    [leftView addSubview:leftImageView];
    leftImageView.centerY = leftView.centerY;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftView;
}

/**
 设置右视图
 @param textField texField
 @param rightView 右视图
 */
-(void)setRightView:(UITextField *)textField rightView:(UIView *)rightView{
    textField.rightViewMode = UITextFieldViewModeAlways;
    textField.rightView = rightView;
}


/**
 初始化标题
 */
-(void)setupTitle{
    NSString *usernamePlaceholder = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_LOGIN_PHONE_PLACEHOLDER];
    if(_type == ZYRecoverPasswordTypeEmail){
        usernamePlaceholder = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ME_MAIN_MODIFY_EMAIL_PLACEHOLDER];
    }
    NSString *passwordPlaceholder = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_FORGET_NEW_PASSWORD_PLACEHOLDER];
    NSString *rePasswordPlaceholder = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_LOGIN_RE_PASSWORD_PLACEHOLDER];
    NSString *codePlaceholder = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_LOGIN_CODE_PLACEHOLDER];
    NSString *confirmBtnTitle = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_CONFIRM_BUTTON_TITLE];
    NSString *codeBtnTitle = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_LOGIN_CODE_BTN_TITLE];
    [_confirmBtn setTitle:confirmBtnTitle forState:UIControlStateNormal];
    [_codeBtn setTitle:codeBtnTitle forState:UIControlStateNormal];
    _usernameTextField.placeholder = usernamePlaceholder;
    _passwordTextField.placeholder = passwordPlaceholder;
    _rePasswordTextField.placeholder = rePasswordPlaceholder;
    _codeTextField.placeholder = codePlaceholder;
}

-(void)setType:(ZYRecoverPasswordType)type{
    _type = type;
    if(type == ZYRecoverPasswordTypePhone){
        _usernameTextField.keyboardType = UIKeyboardTypeNumberPad;
    }else if(type == ZYRecoverPasswordTypeEmail){
        [self setLeftImageView:_usernameTextField imageName:@"ic_login_eamil"];
    }
}

-(void)doConfirm:(id)button{
    if ([self.delegate respondsToSelector:@selector(phoneRecoverView:doConfirm:)]) {
        [self.delegate phoneRecoverView:self doConfirm:button];
    }
}

-(void)doGetCode:(id)sender{
    if ([self.delegate respondsToSelector:@selector(phoneRecoverView:doGetCode:)]) {
        [self.delegate phoneRecoverView:self doGetCode:sender];
    }
    UIButton *codeBtn = sender;
    [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
        [countDownButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [countDownButton setBackgroundImage:[ZYImageUtils imageWithColor:RGBA(191, 191, 191, 1) rect:codeBtn.bounds] forState:UIControlStateNormal];
        NSString *title = [NSString stringWithFormat:@"%zds",second];
        return title;
    }];
    [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
        countDownButton.enabled = YES;
        [countDownButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [countDownButton setBackgroundImage:[ZYImageUtils imageWithColor:kMainColor rect:codeBtn.bounds] forState:UIControlStateNormal];
        return [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_LOGIN_CODE_BTN_TITLE];
    }];
}

#pragma mark - Getter
-(NSString *)getUsername{
    return _usernameTextField.text;
}

-(NSString *)getPassword{
    return _passwordTextField.text;
}

-(NSString *)getRePassword{
    return _rePasswordTextField.text;
}

-(NSString *)getCode{
    return _codeTextField.text;
}
@end
